package com.balabala.gym_system.controller.admin;

import com.balabala.annotation.Permission;
import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_system.config.AdminThreadLocal;
import com.balabala.gym_system.pojo.dto.*;
import com.balabala.gym_system.pojo.vo.AdminRoleVo;
import com.balabala.gym_system.pojo.vo.AdminVo;
import com.balabala.gym_system.service.AdminService;
import com.balabala.gym_system.swagger.utils.WebUtils;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * Author:zl
 * Date:2023/5/1 9:32
 */

@RequestMapping("/admin/admin")
@RestController
@CrossOrigin
@Api(tags = "系统用户管理接口")
public class AdminController {


    @Autowired
    private AdminService adminService;

    /**
     * 条件查询
     *
     * @param adminQueryDto
     * @return
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "条件查询用户以及角色")
    public Result list(@RequestBody AdminQueryDto adminQueryDto) {
        //1.构建分页条件
        PageHelper.startPage(adminQueryDto.getPage(), adminQueryDto.getLimit());
        List<AdminRoleVo> adminList = adminService.findRole(adminQueryDto);
        PageInfo<AdminRoleVo> pageInfo = new PageInfo<>(adminList);
        return Result.ok(adminList, pageInfo.getTotal());

    }

    @ApiOperation(value = "登录", httpMethod = "POST")
    @PostMapping(value = "/login")
    public Result login(@RequestBody @Validated LoginDto loginDto, BindingResult bindingResult) {
        //判断校验是否通过
        if (bindingResult.hasErrors()) {
            return WebUtils.getResult(bindingResult);
        }
        return adminService.login(loginDto);
    }

    @Permission("admin:findAll")
    @GetMapping(value = "/findAll")
    @ApiOperation(value = "查询所有用户")
    public Result findAll() {
        return Result.ok(ResultEnum.GENERALLY_SUCCESS, adminService.list());
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加用户")

    public Result add(@RequestBody @Validated AdminDto adminDto, BindingResult bindingResult) {
        //判断校验是否通过
        if (bindingResult.hasErrors()) {
            return WebUtils.getResult(bindingResult);
        }
        return adminService.insertAdmin(adminDto);
    }

    @GetMapping(value = "/findById/{id}")
    @ApiOperation(value = "id查找用户")
    public Result findById(@PathVariable("id") Long id) {
        AdminVo adminVo = adminService.findById(id);
        return Result.ok(adminVo);
    }

    @Permission("admin:update")
    @PostMapping(value = "/update")
    @ApiOperation(value = "修改用户")
    public Result update(@RequestBody AdminDto AdminDto) {
        return Result.judge(adminService.updateById(AdminDto));
    }

    @Permission("admin:delete")
    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ApiOperation(value = "删除用户")
    public Result deleteById(@RequestParam Long id) {
        return Result.judge(adminService.removeById(id));
    }

    @PostMapping(value = "/updateStatus")
    @ApiOperation(value = "启用禁用用户")
    public Result updateStatus(@RequestBody UpdateStatusDto updateStatusDto) {
        return Result.judge(adminService.updateStatusById(updateStatusDto));
    }

    @GetMapping(value = "/findUserByToken")
    @ApiOperation(value = "根据token获取用户信息")
    public Result findUserByToken() {
        return Result.ok(AdminThreadLocal.get());
    }

    @PostMapping(value = "/updateRoleByIds")
    @ApiOperation(value = "修改用户角色")
    public Result updateRoleByIds(@RequestBody AdminUpdateByIdsDto adminUpdateByIdsDto) {
        adminService.adminUpdateByIds(adminUpdateByIdsDto);
        return Result.ok();
    }
}
